<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <style>
        img{
            width: 40px;
            height: 40px;
        }
    </style>
</head>
<body>
    <div id="app">
        
        <table class="table table-hover">
            <thead>
                <tr>
                    <th>编号</th>
                    <th>电影名</th>
                    <th>评分</th>
                    <th>like</th>
                    <th>点赞</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="item of movies" :key="item._id">
                    <!-- <td v-for="i of item">{{i}}</td> -->
                    <td>{{item._id}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.rating}}</td>
                    <td>{{item.like}}</td>
                    <td >
                        <img @click="handleLike(item._id)"
                            :src="item.like?'images/pause.png':'images/play.png'">
                    </td>
                    <td>
                        <button type="button" class="btn btn-danger" @click="doDelete(item._id)">删除</button>
                        <button type="button" class="btn btn-waring" >修改</button>
                    </td>
                </tr>
            </tbody>
        </table>
        
    </div>
    <!-- <script>
        $.ajax({
            url:"http://localhost:8080/top250"
        }).then(res=>{
            console.log(res)
        })
    </script> -->
    <script>
        new Vue({
            el:"#app",
            data:{
                movies:[]
            },
            mounted() {
                $.ajax({
                    url:"http://localhost:8080/top250"
                }).then(res=>{
                    
                    // console.log(res.data)
                    this.movies = res.data
                })
            },
            methods: {
                doDelete(id){
                    this.movies = this.movies.filter(item=>item._id !=id);
                    $.ajax({
                        url:"http://localhost:8080/doDelete",
                        type:"post",
                        data:{
                            id
                        }
                    }).then(res=>{
                        console.log(res);
                    })
                },
                handleLike(id){
                    
                    var movies = this.movies;
                    var item = movies.find(item=>item._id == id)
                    // console.log(item);
                    if(item.like){
                        item.like = false
                    }else{
                        item.like = true;
                    }
                    // this.movies = movies;
                    /* 发送http请求 */
                    console.log(item.like)
                    console.log(id)
                    $.ajax({
                        url:"http://localhost:8080/doLike",
                        method:"post",
                        data:{
                            id,
                            like:item.like
                        }
                    }).then(res=>{
                        console.log(res)
                    })
                    $.ajax({
                        url:"http://localhost:8080/doCollect",
                        method:"post",
                        data:{
                            id,
                            like:item.like
                        }
                    }).then(res=>{
                        console.log(res)
                    })
                }
            },
        })
    </script>
</body>
</html>